package com.xmindguoguo.boot.core.beetl;

import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;

import com.xmindguoguo.boot.util.ToolUtil;

/**
 * beetl拓展配置,绑定一些工具类,方便在模板中直接调用
 *
 * @author stylefeng
 * @Date 2018/2/22 21:03
 */
public class BeetlConfiguration extends BeetlGroupUtilConfiguration {

    @Autowired
    Environment env;

    @Autowired
    ApplicationContext applicationContext;

//    @Autowired
//    DictSelectorTag dictSelectorTag;

    @Override
    public void initOther() {
        groupTemplate.registerFunctionPackage("shiro", new ShiroExt());
        // 注册通用工具类
        groupTemplate.registerFunctionPackage("tool", new ToolUtil());
        // 注册数据字典
        groupTemplate.registerFunctionPackage("dict", new DictUtil());
        // 注册常引用查询
        groupTemplate.registerFunctionPackage("dbUtil", new DBUtil());

        /*groupTemplate.registerTagFactory("dictSelector", new TagFactory() {
            @Override
            public Tag createTag() {
                return dictSelectorTag;
            }
        });
        
        groupTemplate.registerFunction("env", new Function() {
            @Override
            public String call(Object[] paras, Context ctx) {
                String key = (String) paras[0];
                String value = env.getProperty(key);
                if (value != null) {
                    return getStr(value);
                }
                if (paras.length == 2) {
                    return (String) paras[1];
                }
                return null;
            }
        
            protected String getStr(String str) {
                try {
                    return new String(str.getBytes("iso8859-1"), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            }
        });*/
    }
}
